
{***********************************************************}
{                                                           }
{      Luxena dbExpress eXtension Components                }
{                                                           }
{      TDBXEvents component                                 }
{                                                           }
{      Copyright (c) 2001-2006 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit DBXEvents;

{$I lx.inc}

interface

uses
  Classes;


type

{ TDBXEvents }

  TDBXEvents = class(TComponent)
  private
    FOnObjectCreate: TNotifyEvent;
    FOnObjectDestroy: TNotifyEvent;
    procedure DoObjectCreate(Sender: TObject);
    procedure DoObjectDestroy(Sender: TObject);
  public
    class procedure HandleObjectCreate(Sender: TObject);
    class procedure HandleObjectDestroy(Sender: TObject);
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property OnObjectCreate: TNotifyEvent read FOnObjectCreate write FOnObjectCreate;
    property OnObjectDestroy: TNotifyEvent read FOnObjectDestroy write FOnObjectDestroy;
  end;


implementation

uses SysUtils;


{ TDBXEvents }

var
  globalDBXEventsList: TThreadList;

class procedure TDBXEvents.HandleObjectCreate(Sender: TObject);
var
  I: Integer;
begin
  with globalDBXEventsList.LockList do
    try
      for I := 0 to Count - 1 do
        TDBXEvents(Items[I]).DoObjectCreate(Sender);
    finally
      globalDBXEventsList.UnlockList;
    end;
end;

class procedure TDBXEvents.HandleObjectDestroy(Sender: TObject);
var
  I: Integer;
begin
  with globalDBXEventsList.LockList do
    try
      for I := 0 to Count - 1 do
        TDBXEvents(Items[I]).DoObjectDestroy(Sender);
    finally
      globalDBXEventsList.UnlockList;
    end;
end;

constructor TDBXEvents.Create(AOwner: TComponent);
begin
  inherited;
  globalDBXEventsList.Add(Self);
end;

destructor TDBXEvents.Destroy;
begin
  globalDBXEventsList.Remove(Self);
  inherited;
end;

procedure TDBXEvents.DoObjectCreate(Sender: TObject);
begin
  if Assigned(FOnObjectCreate) then FOnObjectCreate(Sender);
end;

procedure TDBXEvents.DoObjectDestroy(Sender: TObject);
begin
  if Assigned(FOnObjectDestroy) then FOnObjectDestroy(Sender);
end;


initialization
  globalDBXEventsList := TThreadList.Create;

finalization
  FreeAndNil(globalDBXEventsList);

end.